﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Linq;


namespace HRdex_Final.Calendar
{
    public partial class WebForm3 : System.Web.UI.Page, ICallbackEventHandler
    {
        protected DateTime selectedDate;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetData();
            }

            string sbReference = ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context");
            string cbScript = String.Empty;

            //check if the script is already registered or not
            if (!ClientScript.IsClientScriptBlockRegistered("CallServer"))
            {
                cbScript = @" function CallServer(arg,context) { " + sbReference + "}";
                ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", cbScript, true);
            }
        }

        private DataSet GetData()
        {
            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnect = new SqlConnection(connectionString);
            SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM catEvent", myConnect);

            DataSet ds = new DataSet();
            ad.Fill(ds);
            return ds;
        }

        protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
        {
            DataSet ds = GetData();
           
            string s = e.Day.Date.ToShortDateString();
            e.Cell.Text = e.Day.Date.Day.ToString() + "<BR>";


            //date number
            LiteralControl l = new LiteralControl();
            l.Text = e.Day.Date.Day.ToString() + "<BR>";
            e.Cell.Controls.Add(l);
            ////////////

            foreach (DataRow row in ds.Tables[0].Rows)
            {
                string scheduledDate = Convert.ToDateTime(row["date"]).ToShortDateString();
                if (scheduledDate.Equals(s))
                {
                    //for tooltip
                    e.Cell.ToolTip = "Click to see details of this event";
                    string url = e.SelectUrl;
                    e.Cell.Controls.Clear();
                    HyperLink hlink = new HyperLink();
                    hlink.Text = e.Day.Date.Day.ToString();
                    hlink.ToolTip = "Click to see details of this event";
                    e.Cell.Controls.Add(hlink);

                    

                    HyperLink slink = new HyperLink();
                    slink.Text = row["Title"] as String;
                    slink.CssClass = "activity";
                    slink.NavigateUrl = Page.ClientScript.GetPostBackClientHyperlink(CalendarLinkButton, row["EventID"].ToString(), true);
                    e.Cell.Controls.Add(slink);
                    //LinkButton lb = new LinkButton();
                    //lb.Text = link + (int)row["id"] + "'>" + row["Title"] as String + "</a>" + "<BR>";
                    //e.Cell.Controls.Add(lb);
                }
            }


            HtmlAnchor anchor = new HtmlAnchor();
            anchor.InnerHtml = "Add";

            string method = "ShowAddTaskPane(event,'" + e.Day.Date.ToShortDateString() + "')";

            anchor.HRef = "#";
            anchor.Attributes.Add("onclick", method);
            e.Cell.Controls.Add(anchor);

            //HtmlAnchor HAnchor = new HtmlAnchor();
            //HAnchor.InnerHtml = link;


        }

        protected void Btn_AddTask_Command(object sender, CommandEventArgs e)
        {
            string title = txtTitle.Text;
            string details = txtdetails.Text;
            DateTime selDate = DateTime.Parse(Session["date"] as String);


            AddTask(selDate, title, details);

            txtTitle.Text = String.Empty;
            txtdetails.Text = String.Empty;
        }

        private void AddTask(DateTime selDate, string title, string details)
        {
            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\HRdexDB.mdf;Integrated Security=True;User Instance=True";
            SqlConnection myConnection = new SqlConnection(connectionString);

            SqlCommand myCommand = new SqlCommand("INSERT INTO catEvent(date,Title,Description) VALUES(@date,@Title,@Description)", myConnection);
            myCommand.Parameters.AddWithValue("@date", selDate);
            myCommand.Parameters.AddWithValue("@Title", title);
            myCommand.Parameters.AddWithValue("@Description", details);
            


            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myConnection.Close();

        }


        public string GetCallbackResult()
        {
            return String.Empty;
        }

        public void RaiseCallbackEvent(string eventArgument)
        {
            Session["date"] = eventArgument;
            selectedDate = DateTime.Parse(eventArgument);
        }

        protected void CalendarLinkButton_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request.Form["__EVENTARGUMENT"]);

            DataClassesDataContext db = new DataClassesDataContext();
            var activity = (from a in db.catEvents where a.EventID == id select a).Single();

            ResultsLabel.Text = String.Format("{0}", activity.Description);
        }
    }
}